package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.SMSUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author TJH
 * @date 2019-12-14 10:36
 */

@RestController
@RequestMapping("/member")
public class MemberController {

    @Reference
    private MemberService memberService;

    @RequestMapping("/addMember")
    public Result add(@RequestBody Map<String,Object> map){
        //获取收手机号
        String phoneNumber = (String)map.get("phoneNumber");
        Result result;
        try{
            Member byTelephone = memberService.findByTelephone(phoneNumber);
            //若手机号已被注册，则注册失败
            if(byTelephone != null){
                 return new Result(false,"该手机号已被注册");
            }
            memberService.add(map);
            result = new Result(true,"添加会员档案成功");
        }catch (Exception e){
            e.printStackTrace();
            result = new Result(false,"添加会员失败");
        }

        if (result.isFlag()) {
            //添加成功，发送密码
            String password = (String) map.get("password");
            try {
                SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, phoneNumber, password);
            } catch (ClientException e) {
                e.printStackTrace();
                System.out.println("发送短信失败");
            }
        }
        return result;
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult= memberService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS,pageResult);
        }catch(Exception e){
            e.printStackTrace();
        }

        return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
    }

    /**
     * 根据memberId删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            memberService.deleteMember(id);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }catch(Exception e){
            e.printStackTrace();
        }

        return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
    }

    @RequestMapping("/deleteByIds")
    public Result delete(Integer[] MemberIds){
        try {
            memberService.deleteMemberByIds(MemberIds);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }catch(Exception e){
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
    }

    @RequestMapping("/findMemberById")
    public Result findMemberById(@RequestParam("id") Integer memberId){
        try {
            Map<String,Object> map = memberService.findMemberById(memberId);
            return new Result(true,"回写会员档案数据成功",map);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,"回写会员档案数据失败");
        }
    }

    @RequestMapping("/editMember")
    public Result editMember(@RequestBody Map<String,Object> map){
        try{
            memberService.editMember(map);
            return new Result(true,"修改成功",map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"修改失败");
        }
    }

    /**
     * 查询管理师
     * @param username
     * @return
     */
    @RequestMapping("/findUsername")
    public Result findUsernameByRoleName(String username){
        try{
            List<String> list = memberService.findUsername(username);
            return new Result(true,"查询管理师成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询管理师失败");
        }
    }

}
